﻿
@{
    ViewBag.Title = "CityOpenManage";
    Layout = "~/Views/Shared/layoutCommon.cshtml";
}


<section class="content-header">
    <h1>
        省市业务开通
        <small></small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i>主页</a></li>
        <li class="active">省市业务开通</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div id="mainCitySearch"></div>
                <!-- /.box-header -->
                <div class="box-body table-responsive no-padding">
                    <table class="table table-hover body-table-head">
                        <thead>
                            <tr>
                                <th>
                                    城市ID
                                </th>
                                <th>
                                    城市名称
                                </th>
                                <th>
                                    开通状态
                                </th>
                                <th>
                                    开通时间
                                </th>
                                <th>
                                    修改时间
                                </th>
                                <th class="t_c">
                                    操作
                                </th>
                            </tr>
                        </thead>
                        <tbody id="tbodylist"></tbody>
                    </table>
                </div>
                <div class="no-data" id="nodata" style="display:none;">
                    <div class="img-box"></div>
                    <div class="text">暂无数据</div>
                </div>
                <div class="box-footer clearfix ">


                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>

    </div>
</section>
@section scriptsBottom{
    <script src="@Url.Content("~/lib/art-template/template-web.js")"></script>
    <script src="@Url.Content("~/js/citysearch.js")"></script>
    <script id="dataItemTpl" type="text/html">
        {{each data}}
        <tr>
            <td>
                {{$value.code}}
            </td>
            <td>
                {{$value.name}}
            </td>
            <td>
                {{if $value.status==1}}
                    已开通
                {{else}}
                    未开通
                {{/if}}
            </td>
            <td>{{$value.ctime}}</td>
            <td>
                {{if $value.utime!='0001-01-01 00:00:00'}}
                    {{$value.utime}}
                {{/if}}
            </td>
            <td class="t_c">
                {{if $value.status==1}}
                <button type="button" class="btn btn-danger btn-xs" onclick="closeCity('{{$value.code}}','{{$value.name}}')">关闭</button>
                {{else}}
                <button type="button" class="btn btn-info btn-xs" onclick="openCity('{{$value.code}}','{{$value.name}}')">开通</button>
                {{/if}}
                <button type="button" class="btn btn-warning btn-xs" onclick="configBusiness('{{$value.code}}','{{$value.pcode}}','{{$value.name}}')">配置业务</button>
            </td>
        </tr>
        {{/each}}
    </script>
    <script type="text/javascript">
        var defaultid = '@ViewBag.ProvinceId';
        var provinceId = defaultid == '' ? 2 : defaultid;//默认省
        $(document).ready(function () {
            var obj = new CitySearch({
                trigger: '#mainCitySearch',
                pid: provinceId,
                callback: function (pid) {
                    provinceId = pid;
                    search(pid);
                }
            });

            search(provinceId);

        });

        //查询
        function search(pid) {
            $("#nodata").hide();
            if (citydata.length < 1) {
                $("#nodata").show();
                document.getElementById('tbodylist').innerHTML = "";
                return;
            }
           
            BMCW.ajax({
                url: "@Url.Content("~/Config/GetCityOpenCityByPid?provinceId=")" + provinceId,
                type: "GET",
                success: function (o) {
                    if (o.status == 0) {
                        var data = getData(o.data, pid);
                        var html = template('dataItemTpl', data);
                        document.getElementById('tbodylist').innerHTML = html;
                    } else {
                        $("#nodata").show();
                        document.getElementById('tbodylist').innerHTML = "";
                    }
                }
            });



        }
        //获取绑定数据
        function getData(o, pid) {
            var arr = [];
            $.each(citydata[pid], function (i, name) {
                var ctime = '';
                var utime = '';
                var status = 0;
                var obj = getOpenCityInfo(o, i);
                if (obj != null && i == obj.CityID) {
                    ctime = obj.CreatedDate;
                    utime = obj.UpdatedDate;
                    status = obj.Status;
                    cityid = obj.CityID;
                }
                arr.push({
                    code: i,
                    name: name,
                    ctime: ctime,
                    utime: utime,
                    status: status,
                    pcode: pid
                });
            });
            var data = { data: arr };
            return data;
        }

        function getOpenCityInfo(list, cityid) {
            var obj = null;
            $.each(list, function (i) {
                if (list[i].CityID == cityid) {
                    obj = list[i];
                    return;
                }
            });
            return obj;
        }

        //开通
        function openCity(cityId, cityName) {
            BMCW.DataConfirm("确认要开通" + cityName + "?", function () {
                BMCW.ajax({
                    url: "@Url.Content("~/Config/PassCity")",
                    type: "Post",
                    data: {
                        CityId: cityId,
                        CityName: cityName
                    },
                    success: function (o) {
                        if (o.status == 0) {
                            if (o.data == true) {
                                BMCW.msg("开通成功", function () {
                                    search(provinceId);
                                });
                            } else {
                                BMCW.alert("开通失败");
                            }
                        } else {
                            BMCW.alert("开通失败");
                        }
                    }
                });
            })
        }

        //关闭
        function closeCity(id, name) {
            BMCW.DataConfirm("确认要关闭" + name + "?", function () {
                BMCW.ajax({
                    url: "@Url.Content("~/Config/CloseCity")",
                    type: "Post",
                    data: {
                        cityId: id
                    },
                    success: function (o) {
                        if (o.status == 0) {
                            if (o.data == true) {
                                BMCW.msg("关闭成功", function () {
                                    search(provinceId);
                                });
                            } else {
                                BMCW.alert("关闭失败");
                            }
                        } else {
                            BMCW.alert("关闭失败");
                        }
                    }
                });
            })
        }

        function configBusiness(cityId, provinceId, cityName) {
            location.href = "@Url.Content("~/Config/FormBusiness")?cityid=" + cityId + "&provinceid=" + provinceId + "&cityname=" + cityName;
        }
    </script>
}

